package com.example.administrator.yanfoxconn.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.adapter.CanYinAdapter;
import com.example.administrator.yanfoxconn.bean.CanYinInfo;
import com.example.administrator.yanfoxconn.bean.JVSchoolInfo;
import com.example.administrator.yanfoxconn.constant.Constants;
import com.example.administrator.yanfoxconn.utils.BaseActivity;
import com.example.administrator.yanfoxconn.utils.HttpUtils;
import com.example.administrator.yanfoxconn.utils.TimeDateUtils;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 用途：餐飲巡檢主界面
 * Created by Song on 2021/9/3.
 */
public class CanYinMainActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private final int MESSAGE_SET_LIST = 7;//掃描成功賦值
    private final int MESSAGE_SET_SCHOOL = 6;//賦值學校
    private final int MESSAGE_NETMISTAKE = 2;//掃描失敗彈出框
    private final int MESSAGE_UP = 3;//提交信息回復

    @BindView(R.id.tv_title)
    TextView tvTitle;//標題
    @BindView(R.id.btn_title_left)
    Button btnBack;//返回
    @BindView(R.id.lv_btn)
    ListView lvBtn;//列表

    private List<CanYinInfo> infos;
    private String id,result;//餐廳id
    private CanYinAdapter canYinAdapter;

    private int  hour, minute;
    private String time;

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canyin_main);
        ButterKnife.bind(this);
            initDateTime();
        btnBack.setVisibility(View.VISIBLE);
        btnBack.setOnClickListener(this);
        lvBtn.setOnItemClickListener(this);
        result = getIntent().getStringExtra("result");

        tvTitle.setText(result.split(",")[1]);
        getBtnList(result.split(",")[0].replace("\uFEFF", ""));

        }

    private void getBtnList(String id){
            new Thread() {
                @Override
                public void run() {
                    String url2 = Constants.HTTP_CY_MAIN_TYPE + "?r_id=" + id;

                    //把网络访问的代码放在这里
                    String result = HttpUtils.queryStringForPost(url2);

                    Log.e("---------", "url2==fff===" + url2);
                    dismissDialog();
                    Gson gson = new Gson();
                    if (result != null) {
                        Log.e("---------", "result==fff===" + result);
                        JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                        String errCode = jsonObject.get("errCode").getAsString();
                        if (errCode.equals("200")) {
                            JsonArray array = jsonObject.get("data").getAsJsonArray();
                            infos = new ArrayList<CanYinInfo>();

                            for (JsonElement type : array) {
                                CanYinInfo humi = gson.fromJson(type, CanYinInfo.class);
                                infos.add(humi);
                            }
                            Message message = new Message();
                            message.what = MESSAGE_SET_LIST;
                            message.obj = jsonObject.get("errMessage").getAsString();
                            mHandler.sendMessage(message);

                        }
                    } else {
                        Message message = new Message();
                        message.what = MESSAGE_NETMISTAKE;
                        mHandler.sendMessage(message);
                        finish();
                    }
                }
            }.start();

    }

    Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        public void handleMessage(Message msg) {
            switch (msg.what) {

                case Constants.MESSAGE_TOAST://Toast彈出
                    ToastUtils.showLong(CanYinMainActivity.this, msg.obj.toString());
//                    finish();
                    break;
                case Constants.MESSAGE_SET_TEXT://text賦值
                    //setText();
//                    aboutAlert(msg.obj.toString(),MESSAGE_SET_TEXT);
                    break;
                case Constants.MESSAGE_UP://提交響應
//                    worningAlert(msg.obj.toString(),Constants.MESSAGE_TOAST);
//                    ToastUtils.showLong(CrossScanActivity.this, msg.obj.toString());
                    break;
                case Constants.MESSAGE_ALERT://提交響應
//                    worningAlert(msg.obj.toString(),Constants.MESSAGE_TOAST);
//                    ToastUtils.showLong(CrossScanActivity.this, msg.obj.toString());
                    break;
                case MESSAGE_SET_LIST:

                    canYinAdapter  = new CanYinAdapter(CanYinMainActivity.this,infos,hour+":"+minute);
                    lvBtn.setAdapter(canYinAdapter);

                    break;
//                case MESSAGE_SET_ZS:
//                    setZS();
//                    break;

//                case MESSAGE_SHOW://顯示提醒
//                    setText();
//                    tvShow.setVisibility(View.VISIBLE);
//                    tvShow.setText(msg.obj.toString());
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_title_left://返回
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        if(TimeDateUtils.minutesDeviation(infos.get(position).getContent(),time)>0&&TimeDateUtils.minutesDeviation(time,infos.get(position).getRemark())>0){
            Intent intent = new Intent(CanYinMainActivity.this,CanYinSecondActivityNew.class);
            intent.putExtra("flag",infos.get(position).getFlag());
            intent.putExtra("result",result);
            intent.putExtra("mainName",infos.get(position).getName());
            startActivity(intent);
        }else if (infos.get(position).getYC().equals("N")){
            ToastUtils.showShort(CanYinMainActivity.this,"未到時間！");
        }else if (infos.get(position).getYC().equals("A")){
            ToastUtils.showShort(CanYinMainActivity.this,"已完成！");
        }else if (infos.get(position).getYC().equals("Y")){
            ToastUtils.showShort(CanYinMainActivity.this,"有遺漏！");
        }
    }
    private void initDateTime() {
        Calendar calendar = Calendar.getInstance();
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        time = hour+":"+minute;
        Log.e("-------","time=="+time);
    }
    public String remove(String resource,char ch){
        StringBuffer buffer = new StringBuffer();
        int position = 0;
        char currentChar;
        while (position<resource.length()){
            currentChar = resource.charAt(position++);
            if (currentChar!=ch){buffer.append(currentChar);}
        }
        return buffer.toString();
    }
}
